﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms.VisualStyles;
using System.Collections.Generic;
namespace eTMS.Controls
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class MdiTab : System.Windows.Forms.Control
    {
        private System.Windows.Forms.Control tabHeader = new ControlBox();
        private System.Windows.Forms.Control tabControl = new ControlBox();
        private System.Windows.Forms.Panel tabContainer = new Panel();
        private TabHeaderCollection headers = new TabHeaderCollection();
        //private TabButton closebutton=new TabButton();
        private TabButton leftbutton;
        private TabButton rightbutton;
        private TabHeader activeheader = null;
        private TabHeader lastvisbleheader = null;
        private int tabWidth = 0;
        private int leftTab = 0;
        private bool IsInternalChange = false;
        private Color borderColor = Color.Gray;
        private ContextMenuStrip contextMenuStrip1;
        private System.ComponentModel.IContainer components;
        private ToolStripMenuItem mnuDong;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripMenuItem mnuDongTat;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripMenuItem mnuDongTatTruHienTai;
        public event EventHandler OnActiveFormChanged;
        public Color BorderColor
        {
            get
            {
                return borderColor;
            }
            set
            {
                borderColor = value;
            }
        }
        Color tabheaderColor = SystemColors.ControlDarkDark;
        public Color TabHeaderColor
        {
            get
            {
                return tabheaderColor;
            }
            set
            {
                tabheaderColor = value;
                this.tabHeader.BackColor = tabheaderColor;
                this.tabControl.BackColor = tabheaderColor;
            }
        }


        public Form[] Forms
        {
            get
            {
                Form[] frms = new Form[this.headers.Count];
                for (int i = 0; i < this.headers.Count; i++)
                {
                    TabHeader header = this.headers[i];
                    frms[i] = header.Content;
                }
                return frms;
            }
        }
        public MdiTab()
        {
            InitializeComponent();
            //
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            Graphics g = Graphics.FromHwnd(this.Handle);
            SizeF size = g.MeasureString("Hg", this.Font);
            this.tabHeader.Paint += new PaintEventHandler(tabHeader_Paint);

            this.tabHeader.Height = (int)size.Height + (int)size.Height;
            this.tabHeader.Width = this.Width;
            //tabHeader.Dock=DockStyle.Top;
            this.tabHeader.BackColor = tabheaderColor;
            this.tabContainer.BackColor = SystemColors.ControlDark;
            //tabContainer.Dock=DockStyle.Fill;	
            this.tabContainer.BorderStyle = BorderStyle.None;
            this.leftbutton = new TabButton(tabControl);
            this.rightbutton = new TabButton(tabControl);
            this.Controls.Add(this.tabContainer);
            this.Controls.Add(this.tabHeader);
            this.tabControl.Width = rightbutton.Width + leftbutton.Width + 8;//+closebutton.Width+;
            this.tabControl.Height = this.tabHeader.Height;
            this.tabControl.Left = this.tabHeader.Width - this.tabControl.Width;
            this.tabControl.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            this.tabControl.Paint += new PaintEventHandler(tabHeader_Paint);
            this.tabHeader.Controls.Add(this.tabControl);
            this.tabControl.BackColor = tabheaderColor;
            this.leftbutton.ButtonType = ButtonTypeEnum.Left;
            this.rightbutton.ButtonType = ButtonTypeEnum.Right;
            this.leftbutton.Top = (this.tabHeader.Height - this.leftbutton.Height) / 2;
            this.leftbutton.Left = 4;
            this.rightbutton.Top = (this.tabHeader.Height - this.rightbutton.Height) / 2;
            this.rightbutton.Left = this.leftbutton.Left + this.leftbutton.Width + 2;
            this.rightbutton.Enabled = false;
            this.leftbutton.Enabled = false;
            this.rightbutton.Click += new EventHandler(rightbutton_Click);
            this.leftbutton.Click += new EventHandler(leftbutton_Click);
            this.tabHeader.ContextMenuStrip = this.contextMenuStrip1;
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.tabHeader.Top = 1;
            this.tabHeader.Left = 1;
            this.tabHeader.Width = this.Width - 2;
            this.tabContainer.Top = this.tabHeader.Height + 1;
            this.tabContainer.Left = 1;
            this.tabContainer.Height = this.Height - this.tabHeader.Height - 2;
            this.tabContainer.Width = this.Width - 2;
            tabContainer.Refresh();
            InitTabPos(0);
        }

        public void AddControlToContainer(Control control)
        {
            this.tabContainer.Controls.Add(control);
        }
        public void AddForm(Form frm)
        {
            if (this.Parent == null) return;
            System.Windows.Forms.Control ctr = this.Parent;
            while (ctr.Parent != null) ctr = ctr.Parent;
            System.Windows.Forms.Form mdiparent = (Form)ctr;
            frm.MdiParent = mdiparent;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Parent = this.tabContainer;
            frm.Closing += new System.ComponentModel.CancelEventHandler(frm_Closing);
            frm.Size = this.tabContainer.Size;
            frm.Dock = DockStyle.Fill;
            frm.Show();
            TabHeader header = new TabHeader();
            header.Top = 2;
            header.Height = this.tabHeader.Height - 6;
            header.Title = frm.Text;
            header.Content = frm;
            this.tabHeader.Controls.Add(header);
            Graphics g = Graphics.FromHwnd(header.Handle);
            Font font = new Font(header.Font, FontStyle.Bold);
            SizeF size = g.MeasureString(header.Title, font);
            font.Dispose();
            header.Width = (int)size.Width + 40;
            this.headers.AddTab(header);
            header.OnActive += new EventHandler(header_OnActive);
            header.Active();
            tabWidth += header.Width;
            InitTabPos(headers.Count - 1);
            this.tabHeader.Refresh();
            //Add menu
            if (contextMenuStrip1.Items.Count == 5)
            {
                ToolStripSeparator slp = new ToolStripSeparator();
                contextMenuStrip1.Items.Add(slp);
                ToolStripMenuItem pitem = new ToolStripMenuItem();
                contextMenuStrip1.Items.Add(pitem);
                pitem.Text = "Chuyển đến";
            }
            ToolStripMenuItem item = new ToolStripMenuItem();
            ((ToolStripMenuItem)contextMenuStrip1.Items[6]).DropDownItems.Add(item);
            item.Text = frm.Text;
            item.Tag = header;
            item.Click += new EventHandler(item_Click);
            item.CheckOnClick = true;
            header.Tag = item;
            frm.BringToFront();
            foreach (Control control in tabContainer.Controls)
            {
                if (!(control is Form))
                {
                    control.Visible = false;
                }
            }
        }

        void item_Click(object sender, EventArgs e)
        {
            if (((ToolStripMenuItem)sender).Checked)
            {
                TabHeader header = (TabHeader)((ToolStripMenuItem)sender).Tag;
                header.Active();
            }

        }

        public System.Windows.Forms.Form ActiveForm
        {
            get
            {
                if (this.activeheader == null) return null;
                return this.activeheader.Content;
            }
        }
        public void ActiveContent(Form content)
        {
            for (int i = 0; i < headers.Count; i++)
            {
                TabHeader header = headers[i];
                if (header.Content == content)
                {
                    header.Active();
                    break;
                }
            }
        }
        public void CloseActiveForm()
        {
            if (this.activeheader != null)
            {
                this.activeheader.Content.Close();
            }
        }
        private void header_OnActive(object sender, EventArgs e)
        {
            TabHeader header = (TabHeader)sender;
            if ((activeheader != null) && (activeheader != header)) activeheader.DeActive();
            activeheader = header;
            if (OnActiveFormChanged != null) OnActiveFormChanged(null, null);
            this.tabHeader.Refresh();
            if (header.Tag != null)
            {
                IsInternalChange = true;
                foreach (ToolStripMenuItem item in ((ToolStripMenuItem)contextMenuStrip1.Items[4]).DropDownItems)
                {
                    item.Checked = false;
                }
                ((ToolStripMenuItem)header.Tag).Checked = true;
                IsInternalChange = false;
            }
            //this.closebutton.Enabled=true;
        }
        private void InitTabPos(int index)
        {
            int left = 0;
            bool findlass = false;
            if (index > 0)
                left = headers[index - 1].Right;
            else
                index = 0;
            if (headers.Count > 0)
                lastvisbleheader = headers[headers.Count - 1];
            for (int i = index; i < headers.Count; i++)
            {
                TabHeader header = headers[i];
                header.Left = left-1;
                left += header.Width-1;
                if (!findlass && left > this.tabHeader.Width - this.tabControl.Width)
                {
                    findlass = true;
                    lastvisbleheader = header;
                }
            }
            this.leftbutton.Enabled = leftTab < 0;
            this.rightbutton.Enabled = (left >= this.tabHeader.Width - this.tabControl.Width);
            //this.closebutton.Enabled=this.headers.Count>0;
        }
        private void MoveTab(int left)
        {
            if (left > 0) left = 0;
            this.leftbutton.Enabled = left < 0;
            bool findlass = false;
            if (headers.Count > 0)
                lastvisbleheader = headers[headers.Count - 1];
            for (int i = 0; i < headers.Count; i++)
            {
                TabHeader header = headers[i];
                header.Left = left;
                left += header.Width;
                if (!findlass && left > this.tabHeader.Width - this.tabControl.Width)
                {
                    lastvisbleheader = header;
                    findlass = true;
                }
            }
            this.leftbutton.Enabled = leftTab < 0;
            this.rightbutton.Enabled = (left >= this.tabHeader.Width - this.tabControl.Width);
            this.tabHeader.Refresh();
            //this.closebutton.Enabled=this.headers.Count>0;
        }
        private void frm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            Form frm = (Form)sender;
            TabHeader header = this.headers[frm];
            this.tabHeader.Controls.Remove(header);
            int index = headers.IndexOf(header);
            headers.RemoveTab(header);
            tabWidth -= header.Width;
            if (contextMenuStrip1.Items.Count == 7)
                ((ToolStripMenuItem)contextMenuStrip1.Items[6]).DropDownItems.Remove(((ToolStripMenuItem)header.Tag));
            if (this.headers.Count == 1)
            {
                this.contextMenuStrip1.Items.RemoveAt(6);
                this.contextMenuStrip1.Items.RemoveAt(5);
            }
            header.Dispose();
            activeheader = null;
            if ((index >= 0) && (index >= headers.Count))
            {
                index = headers.Count - 1;
                if (tabWidth < this.tabHeader.Width - tabControl.Width)
                {
                    leftTab = 0;
                    MoveTab(leftTab);
                }

            }
            else
            {
                if (tabWidth < this.tabHeader.Width - tabControl.Width)
                {
                    leftTab = 0;
                    MoveTab(0);
                }
                else
                    InitTabPos(index - 1);
            }

            if (index >= 0) headers[index].Active();
            if (headers.Count == 0)
            {
                foreach (Control control in tabContainer.Controls)
                {
                    if (!(control is Form))
                    {
                        control.Visible = true;
                    }
                }
            }
        }
        internal class TabHeaderCollection
        {
            private System.Collections.ArrayList arrHeader = new System.Collections.ArrayList();
            private System.Collections.ArrayList arrForm = new System.Collections.ArrayList();
            public void AddTab(TabHeader header)
            {
                this.arrHeader.Add(header);
                this.arrForm.Add(header.Content);
            }
            public void RemoveTab(TabHeader header)
            {
                this.arrForm.Remove(header.Content);
                this.arrHeader.Remove(header);
            }
            public TabHeader this[Form content]
            {
                get
                {
                    int index = arrForm.IndexOf(content);
                    if (index < 0) return null;
                    return (TabHeader)arrHeader[index];
                }
            }
            public int IndexOf(TabHeader header)
            {
                int index = -1;
                index = arrHeader.IndexOf(header);
                return index;
            }
            public TabHeader this[int index]
            {
                get
                {
                    return (TabHeader)arrHeader[index];
                }
            }
            public int Count
            {
                get
                {
                    return arrForm.Count;
                }
            }
        }
        internal class ControlBox : System.Windows.Forms.Control
        {

            public ControlBox()
            {
                SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            }
            private void DrawBorder(Graphics g)
            {
                Pen atP = null;
                Pen deatP = null;
                Rectangle rect = this.ClientRectangle;
                SolidBrush textBrush = null;
                Font font = this.Font;
                Color cl1;

                atP = new Pen(SystemColors.InactiveBorder, 1);
                deatP = new Pen(SystemColors.ActiveBorder, 1);

                cl1 = SystemColors.Control;


                SolidBrush br = new SolidBrush(cl1);

                g.FillRectangle(br, rect);
                br.Dispose();

                textBrush = new SolidBrush(SystemColors.MenuText);
                g.DrawLine(deatP, rect.Width - 1, rect.Top, rect.Width - 1, rect.Height);
                g.DrawLine(atP, rect.Left + 1, rect.Top, rect.Left + 1, rect.Height);
                textBrush.Dispose();
                deatP.Dispose();
                atP.Dispose();


                SolidBrush sbr = new SolidBrush(BackColor);

                Rectangle rc = new Rectangle(0, this.Height - 4, this.Width, 4);
                g.FillRectangle(sbr, rc);

                sbr.Dispose();

            }
            protected override void OnPaint(PaintEventArgs e)
            {
                DrawBorder(e.Graphics);
                base.OnPaint(e);
            }
        }
        internal class TabHeader : System.Windows.Forms.Control
        {
            int round = 2;
            public event EventHandler OnActive;
            public bool active = false;
            public bool ishover = false;
            private string title;
            private System.Windows.Forms.Form view = null;
            private TabButton btn;
            private Color activeColor = SystemColors.Control;
            private Color bgColor = SystemColors.Control;
            private Color InactiveBackColor
            {
                get
                {
                    string colorScheme = VisualStyleInformation.ColorScheme;

                    if (colorScheme == "HomeStead" || colorScheme == "Metallic")
                        return SystemColors.GradientInactiveCaption;
                    else
                        return Color.FromArgb(114, 152, 204);//SystemColors.InactiveCaption;
                }
            }
            private Color ActiveTextColor
            {
                get { return SystemColors.ControlText; }
            }

            private Color InactiveTextColor
            {
                get { return SystemColors.ControlText; }
            }
            public Color TabColor
            {
                set
                {
                    bgColor = value;
                }
                get
                {
                    return bgColor;
                }
            }
            public Color ActiveColor
            {
                set
                {
                    activeColor = value;
                }
                get
                {
                    return activeColor;
                }
            }
            public string Title
            {
                get
                {
                    return title;
                }
                set
                {
                    title = value;
                }
            }
            public TabHeader()
            {
                SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
                this.DeActive();
                this.btn = new TabButton(this);
                this.btn.Click += new EventHandler(btn_Click);
            }
            public System.Windows.Forms.Form Content
            {
                set
                {
                    view = value;
                }
                get
                {
                    return view;
                }
            }
            protected override void OnClick(EventArgs e)
            {
                base.OnClick(e);
                this.Focus();
            }
            public void Active()
            {
                this.active = true;
                if (this.view != null) view.Visible = true;
                this.Invalidate();
                if (OnActive != null) OnActive(this, new EventArgs());
                if (btn != null)
                {
                    this.btn.Visible = true;
                }
            }
            public void DeActive()
            {
                this.active = false;
                if (this.view != null)
                    view.Visible = false;
                this.Invalidate();
                if (btn != null)
                {
                    btn.Visible = false;
                }
            }
            protected override void OnGotFocus(EventArgs e)
            {
                base.OnGotFocus(e);
                Active();
            }
            private void DrawBorder(Graphics g)
            {
                Pen atP = null;
                Pen deatP = null;
                Rectangle rect = this.ClientRectangle;
                SolidBrush textBrush = null;
                Font font = this.Font;
                Color cl1;
                Color cl2;
                atP = new Pen(SystemColors.InactiveBorder, 1);
                deatP = new Pen(SystemColors.ActiveBorder, 1);
                ButtonRenderer.DrawButton(g, rect, PushButtonState.Normal);
                g.DrawPath(deatP, GetPathInner());
                if (active)
                {

                    cl1 = InactiveBackColor;
                    cl2 = Color.FromArgb(0, cl1);

                    //cl1 = Color.FromArgb(100, cl1);
                    ColorBlend blendColor = new ColorBlend(8);
                    blendColor.Colors = new Color[] { Color.FromArgb(30, cl1), Color.FromArgb(60, cl1), Color.FromArgb(60, cl1), Color.FromArgb(150, cl1), cl1, Color.FromArgb(255, cl1), Color.FromArgb(255, cl1) };
                    blendColor.Positions = new float[] { 0.0f, 0.3f, 0.4f, 0.6f, 0.8f, 0.93f, 1.0f };
                    LinearGradientBrush br = new LinearGradientBrush(rect, cl1, cl2, LinearGradientMode.Vertical);
                    br.InterpolationColors = blendColor;
                    g.FillRectangle(br, rect);

                    font = new Font(this.Font, FontStyle.Bold);
                    textBrush = new SolidBrush(InactiveTextColor);


                    g.DrawLine(deatP, rect.Width, rect.Top, rect.Width, rect.Height);
                    g.DrawLine(deatP, rect.Left, rect.Top, rect.Left, rect.Height);
                    g.DrawLine(deatP, rect.Left, rect.Top, rect.Width, rect.Top);

                    // br.Dispose();
                }
                else
                {

                    cl1 = InactiveBackColor;
                    cl2 = Color.FromArgb(0, cl1);

                    ///cl1 = Color.FromArgb(255, cl1);

                    ColorBlend blendColor = new ColorBlend(8);
                    blendColor.Colors = new Color[] { Color.FromArgb(30, cl1), Color.FromArgb(60, cl1), Color.FromArgb(60, cl1), Color.FromArgb(150, cl1), cl1, Color.FromArgb(255, cl1), Color.FromArgb(0, cl1) };
                    blendColor.Positions = new float[] { 0.0f, 0.3f, 0.4f, 0.6f, 0.8f, 0.93f, 1.0f };
                    //blendColor.Colors = new Color[] { Color.FromArgb(30, cl1), Color.FromArgb(60, cl1), Color.FromArgb(40, cl1), Color.FromArgb(150, cl1), cl1, Color.FromArgb(255, cl1), Color.FromArgb(0, cl1) };
                    // blendColor.Positions = new float[] { 0.0f, 0.1f, 0.2f, 0.41f, 0.8f, 0.85f, 1.0f };


                    LinearGradientBrush br = new LinearGradientBrush(rect, cl1, cl2, LinearGradientMode.Vertical);
                    br.InterpolationColors = blendColor;
                    g.FillRectangle(br, rect);
                    br.Dispose();

                    textBrush = new SolidBrush(ActiveTextColor);
                    g.DrawLine(deatP, rect.Width, rect.Top, rect.Width, rect.Height);
                    g.DrawLine(deatP, rect.Left, rect.Top, rect.Left, rect.Height);
                    g.DrawLine(deatP, rect.Left, rect.Top, rect.Width, rect.Top);
                }
                if (ishover)
                {
                    Blend blend = new Blend();
                    float[] factArray = { 0.0f, 0.3f, 0.8f, 1.0f };
                    float[] posArray = { 0.0f, 0.4f, 0.6f, 1.0f };
                    // Set Factors and Positions properties of Blend
                    blend.Factors = factArray;
                    blend.Positions = posArray;
                    // Set smoothing mode of Graphics object
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    // Create path and add a rectangle
                    GraphicsPath path = new GraphicsPath();
                    Rectangle rc = new Rectangle(rect.X, rect.Y - rect.Height, rect.Width, 6 * rect.Height);
                    path.AddEllipse(rc);
                    // Create path gradient brush
                    PathGradientBrush rgBrush =
                        new PathGradientBrush(path);
                    // Set Blend and FocusScales properties
                    rgBrush.Blend = blend;
                    rgBrush.FocusScales = new PointF(0.9f, 0.2f);
                    Color[] colors = { Color.FromArgb(10, cl1) };
                    // Set CenterColor and SurroundColors properties
                    rgBrush.CenterColor = Color.FromArgb(220, Color.White);
                    rgBrush.SurroundColors = colors;
                    g.FillEllipse(rgBrush, rc);
                    // Dispose of object
                    //g.Dispose();

                }
                StringFormat strft = new StringFormat();
                strft.Alignment = StringAlignment.Center;
                strft.LineAlignment = StringAlignment.Center;
                rect = new Rectangle(0, 0, rect.Width - this.btn.Width, rect.Height);
                g.DrawString(this.title, font, textBrush, rect, strft);
                textBrush.Dispose();
                //font.Dispose();
                atP.Dispose();
                deatP.Dispose();
                btn.OnPaint(g);
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                DrawBorder(e.Graphics);
            }
            protected override void OnMouseHover(EventArgs e)
            {
                base.OnMouseHover(e);
                ishover = true;
                this.Invalidate();
            }
            protected override void OnMouseLeave(EventArgs e)
            {
                base.OnMouseLeave(e);
                ishover = false;
                this.Invalidate();
            }
            private void btn_Click(object sender, EventArgs e)
            {
                if (this.active)
                {
                    this.Content.Close();
                }
            }
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                if (btn != null)
                {
                    this.btn.Left = this.Width - this.btn.Width - 5;
                    this.btn.Top = (this.Height - this.btn.Height) / 2 + 1;
                }
                if ((this.Width > 0) && (this.Height > 0))
                {
                    Region rg = new Region(this.GetPath());
                    this.Region = rg;
                }
            }
            private GraphicsPath GetPath()
            {
                GraphicsPath path = new GraphicsPath();
                Rectangle rc = ClientRectangle;
                path.AddLine(rc.Left, rc.Top + round, rc.Left, rc.Bottom);
                path.AddLine(rc.Left, rc.Bottom, rc.Right, rc.Bottom);
                path.AddLine(rc.Right, rc.Bottom, rc.Right, rc.Top + round);
                path.AddArc(rc.Right - round, rc.Top, 2 * round, 2 * round, 0, 90);
                path.AddLine(rc.Right - round, rc.Top, round, rc.Top);
                path.AddArc(rc.Left, rc.Top, 2 * round, 2 * round, 270, 0);
                return path;
            }
            private GraphicsPath GetPathInner()
            {
                GraphicsPath path = new GraphicsPath();
                Rectangle rc = ClientRectangle;
                path.AddLine(rc.Left, rc.Top + round, rc.Left, rc.Bottom);
                path.AddLine(rc.Left, rc.Bottom, rc.Right, rc.Bottom);
                path.AddLine(rc.Right - 1, rc.Bottom, rc.Right - 1, rc.Top + round);
                path.AddArc(rc.Right - round - 1, rc.Top, 2 * round + 1, 2 * round + 1, 0, 90);
                path.AddLine(rc.Right - round, rc.Top, round, rc.Top);
                path.AddArc(rc.Left + 1, rc.Top, 2 * round + 1, 2 * round + 1, 270, 0);
                return path;
            }
        }
        internal enum ButtonTypeEnum
        {
            Close, Left, Right
        }
        internal class TabButton
        {
            private bool active = false;
            private bool _Enabled = true;
            private bool _Visible = true;
            private ButtonTypeEnum type = ButtonTypeEnum.Close;
            private int _Left = 0;
            private int _Top = 0;
            private int _Width = 0;
            private int _Height = 0;
            private Rectangle _ClientRectange = new Rectangle();
            private Control _Parent = null;
            public event EventHandler Click;
            private void UpdateSize()
            {
                _ClientRectange.X = _Left;
                _ClientRectange.Y = _Top;
                _ClientRectange.Width = _Width;
                _ClientRectange.Height = _Height;
            }
            public bool Enabled
            {
                get
                {
                    return _Enabled;
                }
                set
                {
                    _Enabled = value;
                }
            }
            public bool Visible
            {
                get
                {
                    return _Visible;
                }
                set
                {
                    _Visible = value;
                }
            }
            public Rectangle ClientRectangle
            {
                get
                {
                    return _ClientRectange;
                }
                set
                {
                    _ClientRectange = value;
                }
            }
            public int Left
            {
                get
                {
                    return _Left;
                }
                set
                {
                    _Left = value;
                    UpdateSize();
                }
            }
            public int Top
            {
                get
                {
                    return _Top;
                }
                set
                {
                    _Top = value;
                    UpdateSize();
                }
            }
            public int Width
            {
                get
                {
                    return _Width;
                }
                set
                {
                    _Width = value;
                    UpdateSize();
                }
            }
            public int Height
            {
                get
                {
                    return _Height;
                }
                set
                {
                    _Height = value;
                    UpdateSize();
                }
            }
            public ButtonTypeEnum ButtonType
            {
                get
                {
                    return type;
                }
                set
                {
                    type = value;
                }
            }
            public void Active()
            {
                this.active = true;
            }
            public void Deactive()
            {
                this.active = false;
            }
            protected void OnMouseMove(MouseEventArgs e)
            {
                if ((!this.active) && Enabled && Visible) this.Active();
            }

            protected void OnClick(MouseEventArgs e)
            {
                if (Click != null && this.active && Enabled && Visible)
                {
                    Click(this, new EventArgs());
                }
            }

            public TabButton(Control parent)
            {

                this.Width = 14;
                this.Height = 14;
                parent.MouseMove += new MouseEventHandler(parent_MouseMove);
                parent.MouseClick += new MouseEventHandler(parent_MouseClick);
                parent.Paint += new PaintEventHandler(parent_Paint);
                parent.MouseLeave += new EventHandler(parent_MouseLeave);
                _Parent = parent;
            }

            void parent_MouseClick(object sender, MouseEventArgs e)
            {
                this.OnClick(e);
            }

            void parent_MouseLeave(object sender, EventArgs e)
            {

                this.active = false;
                _Parent.Invalidate(this.ClientRectangle);
            }

            void parent_Paint(object sender, PaintEventArgs e)
            {
                OnPaint(e.Graphics);
            }

            void parent_MouseUp(object sender, MouseEventArgs e)
            {

            }

            void parent_MouseMove(object sender, MouseEventArgs e)
            {
                bool hit = _ClientRectange.Contains(e.X, e.Y);
                if (this.active != hit)
                {
                    this.active = hit;
                    _Parent.Invalidate(this.ClientRectangle);
                }
            }



            private void DrawButton(Graphics g)
            {
                DrawBorder(g);
                switch (type)
                {
                    case ButtonTypeEnum.Close:
                        DrawClose(g);
                        break;
                    case ButtonTypeEnum.Left:
                        DrawLeft(g);
                        break;
                    case ButtonTypeEnum.Right:
                        DrawRight(g);
                        break;
                }
            }
            public void OnPaint(Graphics g)
            {
                if (_Visible)
                {
                    DrawButton(g);
                }
            }
            private void DrawClose(Graphics g)
            {
                Rectangle rect = this.ClientRectangle;
                Rectangle rc = new Rectangle(rect.Left + 4, rect.Top + 4, rect.Width - 8, rect.Height - 8);
                Pen deatP = new Pen(SystemColors.ControlDarkDark, 1.5f);
                g.DrawLine(deatP, rc.Left, rc.Top, rc.Width + rc.Left, rc.Height + rc.Top);
                g.DrawLine(deatP, rc.Width + rc.Left, rc.Top, rc.Left, rc.Height + rc.Top);
                deatP.Dispose();
            }
            private void DrawLeft(Graphics g)
            {
                Rectangle rect = this.ClientRectangle;
                Rectangle rc = new Rectangle(rect.Left + 3, rect.Top + 4, rect.Width - 6, rect.Height - 8);
                Pen deatP = new Pen(SystemColors.ControlDarkDark, 1.4f);
                SolidBrush brush = new SolidBrush(SystemColors.ActiveCaption);
                Point[] points = new Point[4];
                points[0] = new Point(rc.Left, rc.Top + rc.Height / 2);
                points[1] = new Point(rc.Width + rc.Left, rc.Top);
                points[2] = new Point(rc.Width + rc.Left, rc.Top + rc.Height);
                points[3] = new Point(rc.Left, rc.Top + rc.Height / 2);
                if (this.Enabled)
                {
                    g.FillPolygon(brush, points);
                }
                g.DrawLines(deatP, points);
                deatP.Dispose();
                brush.Dispose();
            }
            private void DrawRight(Graphics g)
            {
                Rectangle rect = this.ClientRectangle;
                Rectangle rc = new Rectangle(rect.Left + 3, rect.Top + 4, rect.Width - 6, rect.Height - 8);
                Pen deatP = new Pen(SystemColors.ControlDarkDark, 1.4f);
                SolidBrush brush = new SolidBrush(SystemColors.ActiveCaption);
                Point[] points = new Point[4];
                points[0] = new Point(rc.Width + rc.X, rc.Top + rc.Height / 2);
                points[1] = new Point(rc.Left, rc.Top);
                points[2] = new Point(rc.Left, rc.Top + rc.Height);
                points[3] = new Point(rc.Width + rc.X, rc.Top + rc.Height / 2);
                if (this.Enabled)
                {
                    g.FillPolygon(brush, points);

                }
                g.DrawLines(deatP, points);
                deatP.Dispose();
                brush.Dispose();
            }
            private void DrawBorder(Graphics g)
            {
                Rectangle rect = this.ClientRectangle;
                rect.Width--;
                rect.Height--;
                if ((active) && (Enabled))
                {
                    using (Pen pen = new Pen(SystemColors.Highlight))
                    {
                        SolidBrush br = new SolidBrush(Color.FromArgb(100, SystemColors.Highlight.R, SystemColors.Highlight.G, SystemColors.Highlight.B));
                        g.FillRectangle(br, rect);
                        br.Dispose();
                        g.DrawRectangle(pen, rect);
                    }
                }
            }
        }

        private void closebutton_Click(object sender, EventArgs e)
        {
            CloseActiveForm();
        }

        private void rightbutton_Click(object sender, EventArgs e)
        {
            if (this.lastvisbleheader != null)
            {
                this.leftTab -= lastvisbleheader.Width;
                MoveTab(leftTab);
            }
        }

        private void leftbutton_Click(object sender, EventArgs e)
        {
            if (this.lastvisbleheader != null)
            {
                this.leftTab += lastvisbleheader.Width;
                MoveTab(leftTab);
            }
        }
        private Color InactiveBackColor
        {
            get
            {
                string colorScheme = VisualStyleInformation.ColorScheme;

                if (colorScheme == "HomeStead" || colorScheme == "Metallic")
                    return SystemColors.GradientInactiveCaption;
                else
                    return Color.FromArgb(114, 152, 204);//SystemColors.InactiveCaption;
            }
        }
        private void tabHeader_Paint(object sender, PaintEventArgs e)
        {
            try
            {

                SolidBrush br = new SolidBrush(Color.FromArgb(150,InactiveBackColor));
                e.Graphics.FillRectangle(br, e.ClipRectangle);
                    
                if (this.headers.Count > 0)
                {
                    if (!sender.Equals(tabControl))
                    {
                        br = new SolidBrush(InactiveBackColor);
                        Pen p = new Pen(SystemColors.InactiveBorder);
                        Rectangle rc = new Rectangle(0, tabHeader.Height - 4, this.tabHeader.Width, 4);
                        e.Graphics.FillRectangle(br, rc);
                        e.Graphics.DrawLine(p, 0, tabHeader.Height - 4, this.activeheader.Left, tabHeader.Height - 4);
                        e.Graphics.DrawLine(p, this.activeheader.Right, tabHeader.Height - 4, this.tabHeader.Width, tabHeader.Height - 4);
                        p.Color = SystemColors.ActiveBorder;
                        e.Graphics.DrawLine(p, 0, tabHeader.Height, this.tabHeader.Width, tabHeader.Height);
                        p.Dispose();
                        br.Dispose();
                    }
                    else
                    {
                        br = new SolidBrush(InactiveBackColor);
                        Pen p = new Pen(SystemColors.InactiveBorder);
                        Rectangle rc = new Rectangle(0, tabHeader.Height - 4, this.tabHeader.Width, 4);
                        e.Graphics.FillRectangle(br, rc);
                       
                        e.Graphics.DrawLine(p,0, tabHeader.Height - 4, this.tabControl.Width, tabHeader.Height - 4);
                        p.Color = SystemColors.ActiveBorder;
                        e.Graphics.DrawLine(p, 0, tabHeader.Height, this.tabHeader.Width, tabHeader.Height);
                        p.Dispose();
                        br.Dispose();
                    }
                }
            }
            catch
            {
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawBorder(e.Graphics);
        }
        private void DrawBorder(Graphics g)
        {
            Pen p = new Pen(this.borderColor);
            Rectangle rc = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            g.DrawRectangle(p, rc);
            p.Dispose();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.mnuDong = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuDongTat = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuDongTatTruHienTai = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuDong,
            this.toolStripSeparator1,
            this.mnuDongTatTruHienTai,
            this.toolStripSeparator2,
            this.mnuDongTat});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(121, 54);
            this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);
            // 
            // mnuDong
            // 
            this.mnuDong.Name = "mnuDong";
            this.mnuDong.Size = new System.Drawing.Size(120, 22);
            this.mnuDong.Text = "Đóng";
            this.mnuDong.Click += new EventHandler(mnuDong_Click);
            // 
            // mnuDongTatTruHienTai
            // 
            this.mnuDongTatTruHienTai.Name = "mnuDongTatTruHienTai";
            this.mnuDongTatTruHienTai.Size = new System.Drawing.Size(120, 22);
            this.mnuDongTatTruHienTai.Text = "Đóng các màn hình còn lại";
            this.mnuDongTatTruHienTai.Click += new EventHandler(mnuDongTatTruHienTai_Click);
            // 
            // mnuDongTat
            // 
            this.mnuDongTat.Name = "mnuDongTat";
            this.mnuDongTat.Size = new System.Drawing.Size(120, 22);
            this.mnuDongTat.Text = "Đóng tất";
            this.mnuDongTat.Click += new EventHandler(mnuDongTat_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(117, 6);
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        public void CloseAllForm()
        {
            while (headers.Count > 0)
            {
                TabHeader header = headers[0];
                header.Content.Close();
            }
        }
        void mnuDongTat_Click(object sender, EventArgs e)
        {
            CloseAllForm();
        }
        void mnuDongTatTruHienTai_Click(object sender, EventArgs e)
        {
            List<Form> ls = new List<Form>();
            for (int i = 0; i < headers.Count; i++)
            {
                TabHeader header = headers[i];
                if (!header.active)
                {
                    ls.Add(header.Content);
                }
            }
            for (int i = 0; i < ls.Count; i++)
            {
                ls[i].Close();
            }
        }

        void mnuDong_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < headers.Count; i++)
            {
                TabHeader header = headers[i];
                if (header.ishover)
                {
                    header.Content.Close();
                    return;
                }
            }
            CloseActiveForm();
        }

        private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.headers.Count == 0)
                e.Cancel = true;
        }

    }
}
